<!--  -->
<template>
  <div class="">
    <div style="margin-bottom: 20px">
      <QueryForm
        v-model="filters"
        :query-form-config="queryFormConfig"
        @handleQuery="getList()"
        @clear="clear"
      />
      <el-button v-if="meta.indexOf('BraceletDevAdd') > -1" type="primary" @click="addBtn">{{
        $t('message.add')
      }}</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="$t('message.sbh')"
        prop="devCode"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.sbmc')"
        prop="name"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.ssfl')"
        prop="depositName"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.yj')"
        prop="depositPrice"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        prop="capacity"
        :label="$t('message.shrl')"
        min-width="180"
      />
      <el-table-column
        align="center"
        :label="$t('message.dqshrl')"
        prop="curNum"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.shlx')"
        min-width="80"
      >
        <template slot-scope="{ row }">
          <span>{{
            row.category == 0 ? translate('IC') : translate('ID')
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.sbzt')"
        min-width="80"
      >
        <template slot-scope="{ row }">
          <!-- <el-switch
            v-model="scope.row.connectStatus"
            v-preventReClick
            :active-value="'1'"
            :inactive-value="'0'"
            @change="upDateStatus(scope.row)"
          /> -->
          <span
            class="lx"
            :class="{ act: row.connectStatus == 1 ? true : false }"
            >{{
              row.connectStatus == 1
                ? translate('zaixian')
                : translate('lixian')
            }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.caozuo')"
        min-width="120"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-preventReClick
              type="text"
              v-if="meta.indexOf('BraceletDevEdit') > -1"
              @click="editBtn(scope.row)"
              >{{ $t('message.edit') }}</el-button
            >
            <el-button
              v-preventReClick
              type="text"
              class="secondColor"
              v-if="meta.indexOf('BraceletDevDelete') > -1"
              @click="delRow(scope.row)"
              >{{ $t('message.del') }}</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="total"
      :page="pagination.page"
      @SizeChange="pageSizeChange"
      @current-change="currentPageChange"
    />
    <el-dialog
      :title="DlogTit"
      :visible.sync="dialogVisible"
      width="35%"
      @close="cancelBra('ruleForm')"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item :label="translate('sbh')" prop="devCode">
          <el-input
            v-model="ruleForm.devCode"
            :placeholder="translate('qsr')"
            style="width: 80%"
          ></el-input>
        </el-form-item>
        <el-form-item :label="translate('sbmc')" prop="name">
          <el-input
            v-model="ruleForm.name"
            :placeholder="translate('qsr')"
            style="width: 80%"
          ></el-input>
        </el-form-item>
        <el-form-item :label="translate('shrl')" prop="capacity">
          <el-input-number
            v-model.number="ruleForm.capacity"
            :placeholder="translate('qsr')"
            :min="1"
            :max="120"
            style="width: 40%"
          ></el-input-number>
        </el-form-item>
        <el-form-item :label="translate('dqshrl')" prop="curNum">
          <el-input-number
            v-model.number="ruleForm.curNum"
            :placeholder="translate('qsr')"
            :min="1"
            :max="120"
            style="width: 40%"
          ></el-input-number>
        </el-form-item>
        <el-form-item :label="translate('yhm')" prop="devUserName">
          <el-input
            v-model="ruleForm.devUserName"
            :placeholder="translate('qsr')"
            style="width: 80%"
          ></el-input>
        </el-form-item>
        <el-form-item :label="translate('password')">
          <el-input
            v-model="ruleForm.devUserPwd"
            :placeholder="translate('qsr')"
            style="width: 80%"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item v-else :label="translate('password')">
          <el-input
            v-model="ruleForm.devUserPwd"
            :placeholder="translate('qsr')"
            style="width: 80%"
          ></el-input>
        </el-form-item> -->
        <!-- <el-form-item :label="translate('yj')" prop="depositPrice">
          <el-input-number
            v-model.number="ruleForm.depositPrice"
            :placeholder="translate('qsr')"
            :min="1"
            style="width: 40%"
          ></el-input-number>
        </el-form-item> -->
        <el-form-item :label="translate('ssfl')" prop="depositId">
          <el-select
            v-model="ruleForm.depositId"
            :placeholder="translate('qxz')"
          >
            <el-option
              v-for="item in braSelect"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="translate('shlx')" prop="category">
          <el-select
            v-model="ruleForm.category"
            :placeholder="translate('qxz')"
          >
            <el-option :label="translate('IC')" value="0"></el-option>
            <el-option :label="translate('ID')" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelBra('ruleForm')">{{
          translate('qx')
        }}</el-button>
        <el-button
          :loading="btnLoading"
          type="primary"
          @click="submitForm('ruleForm')"
          >{{ translate('qd') }}</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import QueryForm from '@/components/QueryForm'
import { queryFormConfig } from './config'
import {
  braDevList,
  braDelete,
  braAdd,
  braEdit,
  braSlect,
  braDetail
} from '@/api'
export default {
  //import引入的组件需要注入到对象中才能使用
  components: { QueryForm },
  data () {
    //这里存放数据
    return {
      queryFormConfig,
      filters: {},
      list: [],
      listLoading: false,
      pagination: {
        page: 1,
        rows: 10
      },
      list: [],
      total: 0,
      DlogTit: '',
      ruleForm: {},
      dialogVisible: false,
      rules: {
        devCode: [
          { required: true, message: this.translate('qsr'), trigger: 'blur' }
        ],
        devUserName: [
          { required: true, message: this.translate('qsr'), trigger: 'blur' }
        ],
        devUserPwd: [
          { required: true, message: this.translate('qsr'), trigger: 'blur' }
        ],
        // depositPrice: [
        //   { required: true, message: this.translate('qsr'), trigger: 'blur' }
        // ],
        name: [
          { required: true, message: this.translate('qsr'), trigger: 'blur' },
          {
            min: 2,
            max: 20,
            message: this.translate('yzfmcd'),
            trigger: 'blur'
          }
        ],
        capacity: [
          { required: true, message: this.translate('qsr'), trigger: 'blur' }
        ],
        curNum: [
          { required: true, message: this.translate('qsr'), trigger: 'blur' }
        ],
        depositId: [
          { required: true, message: this.translate('qxz'), trigger: 'change' }
        ],
        category: [
          { required: true, message: this.translate('qxz'), trigger: 'change' }
        ]
      },
      btnLoading: false,
      braSelect: [],
      propName: 'devUserPwd',
      meta:[]
    }
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  created () {
    this.getList()
    this.meta = this.$route.meta.buttons || []

  },
  methods: {
    getList () {
      this.listLoading = true
      const { pagination, filters } = this
      braDevList({
        pagination,
        param: { ...filters }
      })
        .then(({ recordList, totalCount }) => {
          this.listLoading = false
          this.list = recordList
          this.total = totalCount
        })
        .catch(res => {
          this.listLoading = false
        })
    },
    // 添加设备
    addBtn () {
      this.propName = 'devUserPwd'
      this.getRraSelect()
      this.DlogTit = this.translate('add')
      this.dialogVisible = true
    },
    editBtn (row) {
      this.propName = ''
      this.getRraSelect()
      this.DlogTit = this.translate('edit')
      braDetail({
        param: {
          id: row.id
        }
      })
        .then(({ record }) => {
          console.log('🚀 ~ .then ~ record:', record)
          row.devUserName = record.devUserName
          row.category = record.category
          this.ruleForm = { ...row }
          this.dialogVisible = true
        })
        .catch(res => {
          return false
        })
    },
    // 删除设备
    delRow (row) {
      this.$confirm(this.translate('isdel'), this.translate('ts'), {
        confirmButtonText: this.translate('qd'),
        cancelButtonText: this.translate('qx'),
        type: 'warning'
      }).then(async () => {
        if (this.list.length === 1 && this.pagination.page > 1) {
          this.pagination.page--
        }
        const { errorCode } = await braDelete({
          param: {
            id: row.id
          }
        })
        if (errorCode != '000000') return
        this.$message.success(this.translate('delcg'))
        this.getList()
      })
    },
    // 确认新增
    submitForm (formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          this.btnLoading = true
          if (this.ruleForm.id) {
            braEdit({
              param: {
                ...this.ruleForm
              }
            })
              .then(() => {
                this.btnLoading = false
                this.$message.success(this.translate('editcg'))
                this.getList()
                this.cancelBra('ruleForm')
              })
              .catch(() => {
                this.btnLoading = false
                return false
              })
          } else {
            braAdd({
              param: {
                ...this.ruleForm
              }
            })
              .then(() => {
                this.btnLoading = false
                this.$message.success(this.translate('addcg'))
                this.getList()
                this.cancelBra('ruleForm')
              })
              .catch(() => {
                this.btnLoading = false
                return false
              })
          }
        }
      })
    },
    // 取消
    cancelBra (formName) {
      this.$refs[formName].resetFields()
      this.dialogVisible = false
      this.ruleForm = {}
    },
    clear () {
      this.filters = {}
      this.getList()
    },
    pageSizeChange (val) {
      this.pagination.rows = val
      this.getList()
    },
    currentPageChange (val) {
      this.pagination.page = val
      this.getList()
    },
    // 获取下拉分类
    getRraSelect () {
      braSlect({
        param: {}
      }).then(({ recordList, totalCount }) => {
        this.braSelect = recordList
      })
    }
  }
}
</script>
<style lang="scss" scoped>
//@import url(); 引入公共css类
.lx {
  color: rgba(255, 42, 42, 0.884);
}
.act {
  color: rgba(37, 150, 3, 0.897);
}
.demo-ruleForm{
  ::v-deep .el-form-item__label {
  width: 120px !important; 
 }
}
</style>
